/*
 * Created on May 11, 2005
 */
package org.vectrics.skill.struts;

import java.util.Iterator;

import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobSkillRating;
import org.vectrics.recruiter.job.JobSkillRatingSet;
import org.vectrics.struts.VectricsTagSupport;

/**
 * @author MMoore
 */
public class JobSkillSummaryTag extends VectricsTagSupport {

	private static final long serialVersionUID = 1767624426912830484L;

	private String jobId = null;
	
	/**
	 * 
	 */
	public JobSkillSummaryTag() {
		super();
	}

	
	public Job getJob() {
		Job job = RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
		return(job);
	}
	
	protected String makeStartTag() {
		Table mainTable = new Table();
		JobSkillRatingSet ratingSet = RecruitServiceLocator.getSkillProvider().findJobSkillSet(getJob());
		
		if (ratingSet.getRootRating() != null) {
			this.showSkillRating(mainTable, ratingSet.getRootRating());
/*
			Iterator iter = ratingSet.getRootRating().getChildSkillRatings().iterator();
			while (iter.hasNext()) {
				JobSkillRating childRating = (JobSkillRating)iter.next();
				showSkillRating(mainTable, childRating);
			}
			*/
		}
		
		return(mainTable.toString());
	}

	
	//  ---------------------------------------------------------------------
	//  | Weight      | Description
	//  |--------------------------------------------------------------------
	//  | Conjunction | Children table
	//  |             |
	//  |--------------------------------------------------------------------
	private void showSkillRating(Table table, JobSkillRating skillRating) {
		
		Row row = table.addRow();
		Cell mainCell = row.addCell();
		Table innerTable = new BorderTable();
		innerTable.setWidth("100%");
		mainCell.addElement(innerTable);
		Row topRow = innerTable.addRow();
		Cell weightCell = topRow.addCell();
		weightCell.setWidth(30);
		Cell descriptionCell = topRow.addCell();

		
		// Place values in cell
		weightCell.addElement(TextHelper.getFont("stdFont", skillRating.getLevel().toString()));

		String description = skillRating.getDescription();
		if ((description == null) || (description.trim().length() == 0)) {
			description = skillRating.getSkill().getDescription();
		}
		descriptionCell.addElement(TextHelper.getFont("stdFont", description));
		
		if (skillRating.getChildSkillRatings().size() > 0) {
			Row bottomRow = innerTable.addRow();
			Cell conjCell = bottomRow.addCell();
			Cell childTableCell = bottomRow.addCell();
			if (skillRating.getChildConjunctionType() == JobSkillRating.CONJUNCTION_ADDITIVE) {
				conjCell.addElement(TextHelper.getFont("stdFont", this.getPhrase("phrase.skill.conjunction.additive")));
			} else {
				conjCell.addElement(TextHelper.getFont("stdFont", this.getPhrase("phrase.skill.conjunction.maximum")));
			}
			
			
			Iterator iter = skillRating.getChildSkillRatings().iterator();
			while (iter.hasNext()) {
				JobSkillRating childRating = (JobSkillRating)iter.next();
				Table childTable = new Table();
				childTable.setWidth("95%");
				childTableCell.addElement(childTable);
				showSkillRating(childTable, childRating);
			}
		}
	}
	
	/**
	 * @return Returns the jobId.
	 */
	public String getJobId() {
		return jobId;
	}
	/**
	 * @param jobId The jobId to set.
	 */
	public void setJobId(String jobId) {
		this.jobId = jobId;
	}
}
